package com.woniuxy.job;

import com.woniuxy.dao.ServerOrdersMapper;
import com.woniuxy.pojo.ServerOrders;
import com.woniuxy.service.ServerOrderService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 该类用来做用户超时未确认服务完成后的功能
 */
//@Component
public class TimeoutAutocomplete {

    @Autowired
    private ServerOrdersMapper serverOrdersMapper;

    @Autowired
    private ServerOrderService serverOrderService;

    @Scheduled(cron = "0/5 * * * * ?")
    public void timeoutAutocomplete(){
        System.out.println("定时任务开始。。");
        Date date = new Date();// 新建此时的的系统时间
        Date preDay = getNextDay(date);
        System.out.println(preDay);// 返回昨天天的时间
        //如果服务订单表中的订单完成时间大于等于 当前时间-1天，代表用户超时未点击确认完成
        List<ServerOrders> serverOrders = serverOrdersMapper.timeoutAutocomplete(preDay);
        for (ServerOrders so : serverOrders) {
            //超时默认自动完成该订单(在service层做事物处理)
            //完成订单修改后将服务费用打入商家账户
            serverOrderService.updateServerOrderByOvertime(so.getId(),so.getPrice());
        }
        System.out.println("定时任务结束。。");
    }

    //将当前时间-1天，表示24小时已过，
    public static Date getNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);//-1今天的时间减一天
        date = calendar.getTime();
        return date;
    }

}
